'''
    话题发布端：
        创建发布者时需要设定堆栈大小并承接返回对象
        创建接口对象并设定各项属性
        利用 self.pub.publish(self.msg) 将传递的数据压入堆栈
            发布对象   发布方法  接口对象
'''

import rclpy
from rclpy.node import Node
from my_interfaces.msg import MyHello


class Topic_Publisher(Node):
    def __init__(self, name):
        super().__init__(name)
        self._count = 0
        self.pub = self.create_publisher(MyHello, "chat_hello", 10)     # 创建发布者并设定堆栈大小
        self.timer = self.create_timer(1.0, self.time_callback)         # 设置时间回调函数
        self.get_logger().info("Init Success ! & Start Talking ...")
    
    def time_callback(self):
        self._count += 1
        self.msg = MyHello()                # 创建自定义接口对象
        self.msg.name = "Hello czh !"       # 初始化接口对象属性
        self.msg.id = self._count
        self.pub.publish(self.msg)          # 将接口对象压入发布堆栈完成发送


def main(args=None):
    rclpy.init(args=args)

    node = Topic_Publisher("topic_hello_pub")
    rclpy.spin(node)    # 循环阻塞

    node.destroy_node()
    rclpy.shutdown()
